package org.lboot.ufs.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.core.exception.BusinessException;
import org.lboot.s3.client.S3Client;
import org.lboot.s3.config.OssProperties;
import org.lboot.ufs.entity.UfsFile;
import org.lboot.ufs.params.FileUpdateParams;
import org.lboot.ufs.params.FileUploadParams;
import org.lboot.ufs.service.UfsCtl;
import org.lboot.ufs.service.UfsFileService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @author kindear
 * 文件传输控制器
 */

@Slf4j
@RestController
@RequestMapping("ufs")
@AllArgsConstructor
@Api(tags = "文件传输")
@ConditionalOnProperty(prefix = "ufs.api",name = "disable",havingValue = "true",matchIfMissing = true)
public class UfsTransferController {

    S3Client s3Client;

    UfsFileService fileService;

    OssProperties properties;

    UfsCtl ufsCtl;


    @PostMapping("upload")
    @ApiOperation(value = "文件上传")
    @SneakyThrows
    public ResponseDTO<Object> fileUpload(FileUploadParams params) {
        // 更新结果
        return ResponseDTO.succData(
                ufsCtl.uploadFile(params)
        );
    }

    @PutMapping("update")
    @ApiOperation(value = "文件更新")
    @SneakyThrows
    public ResponseDTO<Object> fileUpdate(FileUpdateParams params){
        UfsFile file = fileService.getOneUnwrap(params.getFileId());
        if (file.getStatus().equals(0)){
            throw new BusinessException(HttpStatus.FORBIDDEN,"文件禁止访问");
        }
        if (file.getReadOnly().equals(1)){
            throw new BusinessException(HttpStatus.FORBIDDEN,"只读文件,修改访问拒绝");
        }
        // 更新结果
        return ResponseDTO.succData(
                //ufsCtl.updateFile(params.getFileId(), params.getFile())
                ufsCtl.updateFile(params.getFileId(), params.getFile().getInputStream())
        );
    }

    @GetMapping("download/{fileId}")
    @ApiOperation(value = "文件下载",notes = "")
    @SneakyThrows
    public ResponseEntity<byte[]> downloadFile(@PathVariable("fileId") String fileId, HttpServletRequest request){
        UfsFile file = fileService.getOneUnwrap(fileId);
        if (file.getStatus().equals(0)){
            throw new BusinessException(HttpStatus.FORBIDDEN,"文件禁止访问");
        }
        return ufsCtl.downloadFile(file,request);
    }

    @GetMapping("preview/{fileId}")
    @ApiOperation(value = "文件预览",notes = "")
    @SneakyThrows
    public ResponseEntity<byte[]> previewFile(@PathVariable("fileId") String fileId, HttpServletRequest request){
        UfsFile file = fileService.getOneUnwrap(fileId);
        if (file.getStatus().equals(0)){
            throw new BusinessException(HttpStatus.FORBIDDEN,"文件禁止访问");
        }
        return ufsCtl.openFile(file,request);
    }


}
